﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
using GLSimulationControl;

namespace _3CMillingSimulator
{
    public partial class Form1 : Form
    {
        private String lastStateTxt;
        private SimulatorControlGL.GetStatesProcessProgress statesProcessProgress;
        private SimulatorControlGL.GetErrorNotify errorNotify;

        private bool showPaths;

        public Form1()
        {
            InitializeComponent();
            this.simulatorControlGL1.TrackballEnabled = false;
            this.showPaths = true;
            this.statesProcessProgress = new SimulatorControlGL.GetStatesProcessProgress( this.setStatesProcessProgress );
            this.errorNotify = new SimulatorControlGL.GetErrorNotify( this.getErrorNotify );
        }

        void getErrorNotify(string errorName)
        {

        }

        void setStatesProcessProgress(int value)
        {
            this.progressToolStripProgressBar.Value = value;
        }

        private void loadPathsToolStripMenuItem_Click(object sender, EventArgs e)
        {
            bool fileChosenSuccessfully = false;

            if (ofd.ShowDialog() == DialogResult.OK)
            {
                fileChosenSuccessfully = true;
            }
            string lbl = this.progressToolStripStatusLabel.Text;
            
            if (fileChosenSuccessfully)
            {
                this.progressToolStripStatusLabel.Text = _3CMillingSimulator.Properties.Resources.PathLoadingProgressFunc.ToString();
                SimulatorControlGL.LoadPathFile( ofd.FileName,this.statesProcessProgress,this.errorNotify );
                this.progressToolStripProgressBar.Value = 0;
            }

            this.progressToolStripStatusLabel.Text = lbl;
        }

        private void playToolStripButton_Click(object sender, EventArgs e)
        {
            AnimationSpeed asw = new AnimationSpeed();
            asw.Timer = this.mainTimer;
            asw.Show();
            this.mainTimer.Start();
            this.lastStateTxt = this.stateLabel.Text = _3CMillingSimulator.Properties.Resources.SimulationStarted.ToString();
        }

        private void pauseToolStripButton_Click(object sender, EventArgs e)
        {
            this.mainTimer.Stop();
            this.lastStateTxt = this.stateLabel.Text = _3CMillingSimulator.Properties.Resources.SimulationPaused.ToString();
        }

        private void stopToolStripButton_Click(object sender, EventArgs e)
        {
 
            this.mainTimer.Stop();
            SimulatorControlGL.RewindAnimation();
            this.lastStateTxt =  this.stateLabel.Text = _3CMillingSimulator.Properties.Resources.SimulationStopped.ToString();
        }

        private void createToolStripMenuItem_Click(object sender, EventArgs e)
        {
            CreateMaterialForm cmf = new CreateMaterialForm();
            if (cmf.ShowDialog() == DialogResult.OK)
            {
                uint width = cmf.Material_Width;
                uint height = cmf.Material_Height;
                uint initDepth = cmf.Material_InitialDepth;
                SimulatorControlGL.CreateSimulation( width, height, initDepth );
                SimulatorControlGL.Render();
            }
        }

        private void TrackballToolStripButton_Click(object sender, EventArgs e)
        {
            this.simulatorControlGL1.TrackballEnabled = !this.simulatorControlGL1.TrackballEnabled;
            if (this.simulatorControlGL1.TrackballEnabled)
            {
                this.lastStateTxt = this.stateLabel.Text;
                this.stateLabel.Text = _3CMillingSimulator.Properties.Resources.TrackballEnabled.ToString();
            }
            else
            {
                this.stateLabel.Text = this.lastStateTxt;
                this.lastStateTxt = _3CMillingSimulator.Properties.Resources.TrackballEnabled.ToString();
            }
        }

        private void createNewToolStripMenuItem_Click(object sender, EventArgs e)
        {
            ToolCreate tc = new ToolCreate();
            if (tc.ShowDialog() == DialogResult.OK)
            {
            }
        }

        private void visualizationToolStripMenuItem_Click(object sender, EventArgs e)
        {
            VisualSettings vs = new VisualSettings();
            vs.SimulationControl = this.simulatorControlGL1;
            vs.Show();
        }

        private void createProcessToolStripMenuItem_Click(object sender, EventArgs e)
        {
            CreateProcess cp = new CreateProcess();
            cp.Show();
        }

        private void mainTimer_Tick(object sender, EventArgs e)
        {
            bool finished =  SimulatorControlGL.PerformAnimationStep(this.statesProcessProgress,this.errorNotify);
            if (finished)
            {
                this.mainTimer.Stop();
                this.simulatorControlGL1.Refresh();
                MessageBox.Show( "finished" );
                return;
            }
            this.xCurrentToolPosLabel.Text = String.Format("{0:F2}", SimulatorControlGL.GetToolX());
            this.yCurrentToolPosLabel.Text = String.Format("{0:F2}", SimulatorControlGL.GetToolY());
            this.zCurrentToolPosLabel.Text = String.Format("{0:F2}", SimulatorControlGL.GetToolZ());
            //this.xCurrentToolPosLabel.PointX = SimulatorControlGL.GetToolX();
            //this.currentToolPositionControl.PointY = SimulatorControlGL.GetToolY();
            //this.currentToolPositionControl.PointZ = SimulatorControlGL.GetToolZ();
            this.instructionNumber.Text = String.Format( "{0:n}", SimulatorControlGL.GetCurrentInstructionNumber() );
            this.simulatorControlGL1.Refresh();
        }

        private void animationSpeedToolStripButton_Click(object sender, EventArgs e)
        {
            AnimationSpeed asf = new AnimationSpeed();
            asf.Timer = this.mainTimer;
            asf.Show();
        }

        private void showPathsToolStripButton_Click(object sender, EventArgs e)
        {
            SimulatorControlGL.SetPathVisualizationVisible( this.showPaths );
            this.showPaths = !this.showPaths;
        }

        private void aboutToolStripMenuItem_Click(object sender, EventArgs e)
        {
            AboutBox ab = new AboutBox();
            ab.Show();
        }

        private void loadGCodeFileToolStripMenuItem_Click(object sender, EventArgs e)
        {

        }

        private void generateFromFileToolStripMenuItem_Click(object sender, EventArgs e)
        {
            GeneratePath gp = new GeneratePath();
            gp.Show();
        }

        private void machineToolStripMenuItem_Click(object sender, EventArgs e)
        {
            MachineSettings ms = new MachineSettings();
            ms.Show();
        }

        private void verifyPathToolStripButton_Click(object sender, EventArgs e)
        {
            string lbl = this.progressToolStripStatusLabel.Text;
            this.progressToolStripStatusLabel.Text = _3CMillingSimulator.Properties.Resources.ProcessStatesProgressFunc;
            SimulatorControlGL.ProcessStates( this.statesProcessProgress,this.errorNotify );
            this.progressToolStripStatusLabel.Text = lbl;
            this.progressToolStripProgressBar.Value = 0;
        }

        private void reduceToolStripButton_Click(object sender, EventArgs e)
        {
            this.simulatorControlGL1.Reduce();
        }

        private void magnifyToolStripButton_Click(object sender, EventArgs e)
        {
            this.simulatorControlGL1.Magnify();
        }

        private void newMaterialToolStripButton_Click(object sender, EventArgs e)
        {
             CreateMaterialForm cmf = new CreateMaterialForm();
             if (cmf.ShowDialog() == DialogResult.OK)
             {
                 uint width = cmf.Material_Width;
                 uint height = cmf.Material_Height;
                 uint initDepth = cmf.Material_InitialDepth;
                 SimulatorControlGL.CreateMaterial( width, height, initDepth );
             }
        }
    }
}
